home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MPW Oberon 2.1168 / OExamples / TubeTest.mod < prev    next >
Encoding:
Text File  |  1995-07-03  |  14.3 KB  |  403 lines  |  [TEXT/MPS ]

  1. (*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Animation Sample Application
  6. #
  7. #    TubeTest
  8. #
  9. #    TubeTest.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #                1.01                9/16/92    GAB
  16. #
  17. #    Components:    TubeTest.p            August 1, 1988
  18. #                TubeTest.r            August 1, 1988
  19. #                TubeTest.make        August 1, 1988
  20. #
  21. #    The TubeTest program is a simple demonstration of how to use the Palette 
  22. #    Manager in a color program.  It has a special color palette that is associated
  23. #    with the main window.  The colors are animated using the Palette Manager 
  24. #    to give a flowing tube effect.  The program is very simple, and the Palette
  25. #    Manager and drawing parts are put in separate subroutines to make it easier
  26. #    to figure out what is happening.
  27. #    
  28. #    The program is still a complete Macintosh application with a Main Event Loop,
  29. #    so there is the extra code to run the MEL.  
  30. #    
  31. #    There is a resource file that is necessary as well, to define the Menus, Window,
  32. #    Dialog, and Palette resources used in the program.  
  33. #
  34. #    See Sample and TESample for the general structure and MultiFinder techniques that
  35. #    we recommend that you use when building a new application.
  36. #
  37. ------------------------------------------------------------------------------*)
  38.  
  39. MODULE TubeTest;
  40.  (*
  41.     File TubeTest.p
  42.  
  43.     Version 1.0: 6/2/88
  44.     
  45.     4/19/88:
  46.     TubeTest -- A small sample application written by Bo3b Johnson.
  47.     The idea is to draw two circles in varying colors in the window, then
  48.     animate the colors when the menu is chosen.    This is a complete program
  49.     with event loop and everything.    It is intended to be a simple example of
  50.     how to use the Palette Manager to do some minor color animation, and
  51.     how to use the PM to set up the colors desired in a window.
  52.     
  53.     Also see the resource file that goes with this to see how the Palette 
  54.     itself is layed out.
  55.     
  56.     Could be built with something like this:
  57.         rez TubeTest.r -o TubeTest
  58.         pascal TubeTest.p
  59.         Link TubeTest.p.o ∂
  60.             "(*MPW*)Libraries:"Interface.o ∂
  61.             "(*MPW*)Libraries:"Runtime.o ∂
  62.             "(*MPW*)PLibraries:"Paslib.o ∂
  63.             -o TubeTest
  64.         TubeTest
  65.  *)
  66.  
  67.  (*Where does it fit:
  68.     This is a series of sample programs for those doing development
  69.     using Color QuickDraw.  Since the whole color problem depends
  70.     upon the exact effect desired, there are a number of answers
  71.     to how to use colors, from the simple to the radically complex.
  72.     These programs try to cover the gamut, so you should use 
  73.     which ever seems appropriate.  In most cases, use the simplest
  74.     one that will give the desired results.  The compatibility
  75.     rating is from 0..9 where low is better.  The more known risks 
  76.     there are the higher the rating.
  77.     
  78.     
  79.     The programs (in order of compatibility):
  80.     
  81.         SillyBalls:
  82.             This is the simplest use of Color QuickDraw, and does
  83.             not use the Palette Manager.  It draws randomly colored
  84.             balls in a color window.  This is intended to give you
  85.             the absolute minimum required to get color on the screen.
  86.             Written in straight Pascal code.
  87.             Compatibility rating = 0, no known risks.
  88.         
  89.         FracAppPalette:
  90.             This is a version of FracApp that uses only the Palette
  91.             Manager.  It does not support color table animation
  92.             since that part of the Palette Manager is not sufficient.
  93.             The program demonstrates a full color palette that is
  94.             used to display the Mandelbrot set.  It uses an offscreen
  95.             gDevice w/ Port to handle the data, using CopyBits to
  96.             draw to the window.  The Palette is automatically 
  97.             associated with each window.  The PICT files are read
  98.             and written using the bottlenecks, to save on memory
  99.             useage.
  100.             Written in MacApp Object Pascal code.
  101.             Compatibility rating = 0, no known risks.
  102.         
  103.         TubeTest:    (***)
  104.             This is a small demo program that demonstrates using the
  105.             Palette Manager for color table animation.  It uses a 
  106.             color palette with animating entries, and draws using the
  107.             Palette Manager.  There are two circles of animating colors
  108.             which gives a flowing tube effect.  This is a valid case
  109.             for using the animating colors aspect of the Palette Manager,
  110.             since the image is being drawn directly.
  111.             Written in straight Pascal code.
  112.             Compatibility rating = 0, no known risks.
  113.         
  114.         FracApp:
  115.             This is the ‘commercial quality’ version of FracApp.  This
  116.             version supports color table animation, using an offscreen
  117.             gDevice w/ Port, and handles multiple documents.  The
  118.             CopyBits updates to the screen are as fast as possible.  The
  119.             program does not use the Palette Manager, except to
  120.             provide for the system palette, or color modes with less than
  121.             255 colors.  For color table animation using an offscreen
  122.             gDevice w/ Port, it uses the Color Manager and handles the
  123.             colors itself.  Strict compatibility was relaxed to allow for
  124.             a higher performance program.  This is the most ‘real’ of the
  125.             sample programs.
  126.             Written in MacApp Object Pascal code.
  127.             Compatibility rating = 2.  (nothing will break, but it may not
  128.             always look correct.)
  129.         
  130.         FracApp300:
  131.             This doesn't support colors, but demonstrates how to create and
  132.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  133.             resolution on LaserWriters, and clipped on other printers (but
  134.             they still print).  It demonstrates how to use a high resolution
  135.             image as a PICT file, and how to print them out.
  136.             Written in MacApp Object Pascal code.
  137.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  138.             out of the ordinary, although supported.)
  139. *)
  140.  
  141.  (*$R-*)    (* No range checking.*)
  142.  (* $ D+*)    (* Debugging labels on. *)
  143.  
  144.  
  145.     (* Interface files with all the happy Macintosh stuff in them. *)
  146.  
  147. IMPORT SYSTEM, Types, Quickdraw, Memory, Windows, Dialogs, Menus,
  148.        Fonts, TextEdit, Events, OSUtils, Palettes, Desk, SegLoad;
  149.     
  150.     
  151. CONST
  152.     appleID                    = 1000;        (* resource IDs/menu IDs for Apple, File and Edit menus *)
  153.     fileID                    = 1001;
  154.     editID                    = 1002;
  155.     
  156.     appleM                    = 0;        (* Index for each menu in myMenus (array of menu handles) *)
  157.     fileM                    = 1;
  158.     editM                    = 2;
  159.  
  160.     menuCount                = 3;        (* Total number of menus *)
  161.  
  162.     windowID                = 1000;        (* Resource ID for main window *)
  163.     aboutMeDLOG             = 1000;        (* And Resource ID for About box dialog. *)
  164.  
  165.     tubularItem                = 1;        (* When checked, animation of colors. *)
  166.     quitItem                = 3;        (* Quit in the menu of course. *)
  167.  
  168.     aboutMeCommand            = 1;        (* Menu item in apple menu for About TubeTest item *)
  169.     
  170.     totalColors                = 152;        (* use 150 colors in our palette for drawing eyes. *)
  171.     numColors                = 150;        (* to skip black and white. *)
  172.     
  173.     
  174.  
  175. VAR
  176.     myMenus:        ARRAY menuCount OF Menus.MenuHandle;
  177.     dragRect:        Types.Rect;                (* Rectangle used to mark bounds for dragging window *)
  178.     doneFlag:        BOOLEAN;            (* TRUE if user has chosen Quit command *)
  179.     myEvent:        Events.EventRecord;
  180.     myWindow:        Windows.WindowPtr;
  181.     whichWindow:    Windows.WindowPtr;
  182.     tubeCheck:        BOOLEAN;            (* if true, the menu is checked, and we animate. *)
  183.     theChar:        CHAR;
  184.     error:             Types.OSErr;
  185.     theWorld:         OSUtils.SysEnvRec;
  186.     
  187.     (* This routine will update the window when required by update events.    It
  188.         will draw two circular dudes that are indexed in colors through the colors
  189.         we are using. 0 and 1 are skipped, since those are white and black in the
  190.         palette. *)
  191. PROCEDURE DrawEyes;
  192.     
  193.     VAR        TempRect:    Types.Rect;
  194.             I:            INTEGER;
  195.     
  196. BEGIN
  197.     Quickdraw.SetRect(TempRect, numColors, numColors, numColors, numColors);
  198.     FOR I := 2 TO totalColors DO
  199.         Palettes.PmForeColor(I);
  200.         Quickdraw.FrameOval (TempRect);
  201.         Quickdraw.InsetRect (TempRect, -1, -1);
  202.     END;
  203.     
  204.     Quickdraw.SetRect(TempRect, numColors*3, numColors, numColors*3, numColors);
  205.     FOR I := totalColors TO 2 BY -1 DO
  206.         Palettes.PmForeColor(I);
  207.         Quickdraw.FrameOval (TempRect);
  208.         Quickdraw.InsetRect (TempRect, -1, -1);
  209.     END;
  210. END DrawEyes;
  211.  
  212.  
  213. PROCEDURE SetUpMenus;
  214.  
  215.     VAR        I:    INTEGER;
  216.  
  217. BEGIN
  218.     (* Read menu descriptions from resource file into memory and store handles
  219.     in myMenus array *)
  220.     myMenus[appleM] := Menus.GetMenu(appleID);    (*read Apple menu from resource file*)
  221.     Menus.AddResMenu(myMenus[appleM], LONG("DRVR"));    (*add desk accessory names to Apple menu*)
  222.     myMenus[fileM] := Menus.GetMenu(fileID);        (*read file menu from resource file*)
  223.     myMenus[editM] := Menus.GetMenu(editID);        (*read edit menu from resource file*)
  224.  
  225.     FOR I := 1 TO menuCount DO Menus.InsertMenu(myMenus[I-1], 0) END; (*install menus in menu bar*)
  226.     
  227.     Menus.DrawMenuBar;                            (*and draw menu bar*)
  228. END SetUpMenus;
  229.  
  230.  
  231.     (* Use the Palette currently attached to the main window to animate the colors 
  232.     in the circular eye shapes.  This will rotate them around to give the flowing 
  233.     tube effect. We make the palette into a color table so we can move entries 
  234.     around.    We have to skip the first two entries since those are black and white. 
  235.     (entries 0 and 1) *)
  236. PROCEDURE ShiftyColors;
  237.     
  238.     VAR        currPalette:    Palettes.PaletteHandle;
  239.             destCTab:        Quickdraw.CTabHandle;
  240.             lastCSpec:        Quickdraw.ColorSpec;
  241.  
  242. (*$MAIN*)    
  243. BEGIN
  244.     Quickdraw.SetPort (myWindow);
  245.     
  246.     currPalette := Palettes.GetPalette(myWindow);
  247.     destCTab := Quickdraw.CTabHandle (Memory.NewHandle (SIZE (Quickdraw.ColorTable)+(totalColors*SIZE(Quickdraw.ColorSpec))));
  248.     IF destCTab = NIL  THEN RETURN END;
  249.     Palettes.Palette2CTab(currPalette, destCTab);
  250.     
  251.         (* Move the colors around in the color table, skipping 0 and 1, and moving
  252.         all the elements down by one, and copying the element at 2 back to the 
  253.         end of the table. The effect is to rotate the colors in the table.    *)
  254.     (*$R- range checking off*)
  255.     lastCSpec := destCTab.ctTable[2];                            (* pull first one off. *)
  256.     SYSTEM.MOVE(SYSTEM.ADR(destCTab.ctTable[3]), SYSTEM.ADR(destCTab.ctTable[2]), 
  257.                 (numColors) * SIZE(Quickdraw.ColorSpec) );                (* copy all one entry down. *)
  258.     destCTab.ctTable[totalColors-1] := lastCSpec;                (* put last color back on front. *)
  259.     (*$R+ range checking on*)
  260.         
  261.     Palettes.AnimatePalette(myWindow, destCTab, 2, 2, numColors);
  262.     
  263.     Memory.DisposHandle (Types.Handle (destCTab));
  264. END ShiftyColors;
  265.  
  266.  
  267.     (* Display the dialog box in response to the 'About TubeTest' menu item. *)
  268. PROCEDURE ShowAboutMeDialog;
  269.  
  270.     VAR        theDialog:    Dialogs.DialogPtr;
  271.             itemHit:    INTEGER;
  272.  
  273. BEGIN
  274.     theDialog := Dialogs.GetNewDialog(aboutMeDLOG, NIL, Windows.WindowPtr( - 1));
  275.     Dialogs.ModalDialog(NIL, itemHit);
  276.     Dialogs.DisposDialog(theDialog);
  277. END ShowAboutMeDialog;
  278.  
  279.  
  280.     (* Execute menu command specified by mResult, the result of MenuSelect *)
  281. PROCEDURE DoCommand(mResult: LONGINT);
  282.  
  283.     VAR        theItem:    INTEGER;                (* menu item number from mResult low-order word *)
  284.             theMenu:    INTEGER;                (* menu number from mResult high-order word *)
  285.             name:         Types.Str255;                    (* desk accessory name *)
  286.             temp:         INTEGER;
  287.             dummy:        BOOLEAN;
  288.  
  289. BEGIN
  290.     theItem := LoWrd(mResult);                    (* call Toolbox Utility routines to *)
  291.     theMenu := HiWrd(mResult);                    (* set menu item number and menu *)
  292.  
  293.     CASE theMenu OF                                (* case on menu ID *)
  294.  
  295.         appleID:
  296.             IF (theItem = aboutMeCommand) THEN  ShowAboutMeDialog
  297.             ELSE                            (* call Menu Manager to get desk acc.*)
  298.                 Menus.GetItem(myMenus[appleM], theItem, name);
  299.                 temp := Desk.OpenDeskAcc(name);
  300.                 Quickdraw.SetPort(myWindow);
  301.             END| (* appleID *)
  302.  
  303.         fileID:
  304.             IF theItem = quitItem THEN doneFlag := TRUE END;
  305.             
  306.             IF theItem = tubularItem THEN
  307.                 tubeCheck := ¬tubeCheck;
  308.                 Menus.CheckItem(myMenus[fileM], tubularItem, tubeCheck);
  309.             END|    (* fileID *)
  310.  
  311.         editID:
  312.             dummy := Desk.SystemEdit(theItem - 1)|(* Pass the command on to the Desk Manager.*)
  313.  
  314.         ELSE (* ignore other menus *)
  315.     END; (*of menu CASE*)
  316.  
  317.     Menus.HiliteMenu(0);                        (* Unhighlight menu title(highlighted by MenuSelect) *)
  318. END DoCommand;
  319.  
  320.  
  321. BEGIN  (* Main *)
  322.     (* Test the computer to be sure we can do color.  If not we would crash, which
  323.     would be bad.  If we can’t run, just beep and exit. *)
  324.     error := OSUtils.SysEnvirons(1, theWorld);
  325.     IF ¬theWorld.hasColorQD THEN
  326.         OSUtils.SysBeep (50);
  327.         SegLoad.ExitToShell;                (* If no color QD, we must leave. *)
  328.     END;
  329.  
  330.     Quickdraw.InitGraf(Quickdraw.thePort);
  331.     Fonts.InitFonts;
  332.     Windows.InitWindows;
  333.     Menus.InitMenus;
  334.     TextEdit.TEInit;
  335.     Dialogs.InitDialogs(NIL);
  336.     Quickdraw.InitCursor;
  337.  
  338.     Quickdraw.SetRect(dragRect, 4, 24, Quickdraw.screenBits.bounds.botRight.h - 4, Quickdraw.screenBits.bounds.botRight.v - 4);
  339.     doneFlag := FALSE;                (* flag to detect when Quit command is chosen*)
  340.     tubeCheck := FALSE;                (* flag for animating color is initially off. *)
  341.  
  342.         (* Open the color window. *)
  343.     myWindow := Windows.GetNewCWindow(windowID, NIL, Windows.WindowPtr(-1));
  344.     Quickdraw.SetPort(myWindow);
  345.  
  346.         (* Set up menus last, since the menu drawing can then use the palette we have for ou
  347.         window. Makes the Apple look better, in particular. *)
  348.     SetUpMenus;
  349.     
  350.     (* Main Event Loop *)
  351.     REPEAT
  352.         Desk.SystemTask;
  353.  
  354.         IF Events.WaitNextEvent(Events.everyEvent, myEvent, 5, NIL) THEN
  355.             CASE myEvent.what OF            (* case on event type*)
  356.  
  357.                 Events.mouseDown:
  358.                     CASE Windows.FindWindow(myEvent.where, whichWindow) OF
  359.  
  360.                         Windows.inSysWindow:                (* desk accessory window: call Desk Manager to handle it*)
  361.                             Desk.SystemClick(myEvent, whichWindow)|
  362.  
  363.                         Windows.inMenuBar:                    (* Menu bar: learn which command, then execute it. *)
  364.                             DoCommand(Menus.MenuSelect(myEvent.where))|
  365.  
  366.                         Windows.inDrag:                        (* title bar: call Window Manager to drag*)
  367.                             Windows.DragWindow(whichWindow, myEvent.where, dragRect)|
  368.  
  369.                         Windows.inContent:                    (* body of application window: *)
  370.                             IF whichWindow # Windows.FrontWindow() THEN
  371.                                 Windows.SelectWindow(whichWindow); (*and make it active if not*)
  372.                             END
  373.                         ELSE (* ignore other events *)
  374.                     END| (*of mouseDown*)
  375.  
  376.                 Events.updateEvt:                            (* Update the eyes in window. *)
  377.                     IF Windows.WindowPtr(myEvent.message) = myWindow THEN
  378.                         Windows.BeginUpdate(Windows.WindowPtr(myEvent.message));
  379.                             DrawEyes;
  380.                         Windows.EndUpdate(Windows.WindowPtr(myEvent.message));
  381.                     END| (*of updateEvt*)
  382.                             
  383.                 Events.keyDown, Events.autoKey:                    (* key pressed once or held down to repeat *)
  384.                     IF myWindow = Windows.FrontWindow() THEN
  385.                         theChar := CHR(BAND(myEvent.message, Events.charCodeMask)); (* get the char *)
  386.                             (* If Command key down, do it as a Menu Command. *)
  387.                         IF BAND(myEvent.modifiers, Events.cmdKey) # 0 THEN DoCommand(Menus.MenuKey(theChar)) END;
  388.                     END; (*of keyDown and autoKey*)
  389.  
  390.                 ELSE (* ignore other events *)
  391.             END; (*of event CASE*)
  392.         END;
  393.  
  394.             (* If we have menu item checked, go ahead and animate colors. *)
  395.         IF tubeCheck THEN ShiftyColors END;
  396.         
  397.     UNTIL doneFlag;
  398.  
  399.         (* clean up after palette manager, so he can chuck the palette in use. *)
  400.     Windows.DisposeWindow (myWindow);
  401.     
  402. END TubeTest.
  403.